<template>
	<view class="page-room">
		<uni-status-bar ></uni-status-bar>
		<cl-sticky is-topbar>

			<!-- 搜索框 -->
			<view class="search">
				<cl-input
					confirmType="搜索"
					prefix-icon="cl-icon-search"
					placeholder="搜索房间号或房主名称"
					round
					@confirm="search()"
				></cl-input>
			</view>

			<!-- tabs -->
			<view class="tabs">
				<view
					class="item"
					:class="{ 'is-active': tabsActive == item.index }"
					v-for="(item, index) in tabs"
					:key="index"
					@tap="activeTabs(item)"
				>
					<text class="label">{{ item.name }}</text>
					<image
						src="@/static/icon/tabs-icon.png"
						mode="aspectFill"
						v-if="tabsActive == item.index"
					/>
				</view>
			</view>
		</cl-sticky>

		<!-- 列表 -->
		<view class="list">
			<cl-row :gutter="30">
				<cl-col :span="12" v-for="(item, index) in list" :key="index">
					<view class="item" @tap="topChat">
						<!-- 封面区域 -->
						<view class="cover">
							<view class="mask"></view>
							<image :src="item.cover" mode="aspectFill" class="cover-img" />

							<!-- 人数，位置 -->
							<view class="top">
								<!-- 人数 -->
								<view class="number">
									<view class="point"></view>
									<text>{{ item.total }}人</text>
								</view>
								<!-- 位置 -->
								<view class="position">
									<cl-icon name="map-fill" color="#DBDBDB"></cl-icon>
									{{ item.city }}
								</view>
							</view>

							<!-- 头像，昵称 -->
							<view class="avatar-group">
								<view class="avatar">
									<image :src="item.user.user_icon" mode="aspectFill" class="img" />
									<view class="man" v-if="item.user.sex == 1">
										<image
											src="@/static/icon/brief-icon-1.png"
											mode="aspectFill"
										/>
									</view>
									<view class="woman" v-else-if="item.user.sex == 2">
										<image
											src="@/static/icon/brief-icon-2.png"
											mode="aspectFill"
										/>
									</view>
								</view>
								<text class="name">{{ item.user.username }}</text>
							</view>
						</view>

						<view class="bottom">
							<!-- 标题 -->
							<text class="title">
								{{ item.name }}
							</text>

							<!-- 标签，副标题 -->
							<view class="group">
								<view class="content">
									<text class="tag">
										{{ item.type_name }}
									</text>
									<text class="subtitle">
										{{ item.summary }}
									</text>
								</view>
							</view>
						</view>
					</view>
				</cl-col>
			</cl-row>
			
			<load-more :loadText="loadText"></load-more>
		</view>

		<!-- 悬浮组件 -->
		<view class="floot" @tap="navigateToCreate()">
			<image src="@/static/icon/floot-icon.png" mode="aspectFill"/>
		</view>
	</view>
</template>

<script>
import uniStatusBar from "../../components/uni-status-bar.vue";
import loadMore from "../../components/load-more.vue";
export default {
	components:{
		uniStatusBar,
		loadMore
	},
	onLoad() {
		this.getTypeList();
		this.getList();
	},
	data() {
		return {
			keywords: "",
			loadText: "加载中...",
			isLoading: false,
			hasMore: true,
			page: 1,
			tabsActive: 0,
			tabs: [],
			list: [],
		};
	},
	onReachBottom(){
		this.getList();
	},
	onPullDownRefresh(){
		this.page = 1;
		this.hasMore = true;
		this.loadText = "加载中...";
		this.list = [];
		this.isLoading = false;
		this.keywords = "";
		this.getList();
	},
	methods: {
		activeTabs(item) {
			this.tabsActive = item.index;
		},
		topChat() {
			uni.navigateTo({
				url: "/pages/chat/index",
			});
		},
		navigateToCreate(){
			uni.navigateTo({
				url:"/pages/chat/create"
			})
		},
		search(){
		},
		async getTypeList(){
			let [err,res] = await this.$http.get('Room/roomTypeLists',{});
			
			if(!this.$http.errorCheck(err,res)){
				return;
			}
			
			this.tabs = res.data.data;
		},
		async getList(){
			if(!this.hasMore || this.isLoading){
				return;
			}
			
			this.isLoading = true;
			
			let param = {
				page: this.page
			}
			if(this.keywords != ""){
				param.keywords = this.keywords;
			}
			
			let [err,res] = await this.$http.get('Room/lists',param);
			
			if(!this.$http.errorCheck(err,res)){
				return;
			}
			
			if(res.data.data.length == 0 || res.data.data.length < 10){
				this.hasMore = false;
				this.loadText = "没有更多了...";
			}
			
			this.page++;
			uni.stopPullDownRefresh();
			this.isLoading = false;
			
			this.list = this.list.concat(res.data.data);
		},
	},
};
</script>

<style lang="scss">
page {
	background-color: #f5f5f5;
}
.page-room {
	.cl-topbar {
		.icon {
			padding-left: 30rpx;
			image {
				width: 46rpx;
				height: 46rpx;
			}
		}
	}
	.search {
		position: relative;
		padding: 30rpx 30rpx;
		background-color: #f5f5f5;
		z-index: 99;

		.cl-input {
			background-color: #ffffff;
			border: 0;
		}
	}
	.tabs {
		display: flex;
		justify-content: space-between;
		position: relative;
		padding: 0 30rpx 10rpx 30rpx;
		background-color: #f5f5f5;
		z-index: 99;
		.item {
			display: flex;
			flex-direction: column;
			align-items: center;
			.label {
				font-size: 32rpx;
				font-weight: 400;
				color: #b1b1b1;
			}
			image {
				display: block;
				width: 44rpx;
				height: 12rpx;
				margin-top: 6rpx;
			}
			&.is-active {
				.label {
					font-size: 40rpx;
					font-weight: 500;
					color: #282828;
				}
			}
		}
	}
	.list {
		margin-top: 27rpx;
		padding: 0 30rpx;
		.item {
			width: 100%;
			margin-bottom: 30rpx;
			padding-bottom: 30rpx;
			background-color: #ffffff;
			border-radius: 16rpx;
			.cover {
				position: relative;
				width: 330rpx;
				height: 330rpx;
				margin-bottom: 30rpx;
				border-radius: 16rpx 16rpx 0 0;
				overflow: hidden;
				.top {
					display: flex;
					justify-content: space-between;
					align-items: center;
					position: absolute;
					top: 31rpx;
					left: 0;
					width: 100%;
					padding: 0 24rpx;
					box-sizing: border-box;
					z-index: 5;

					.number {
						display: flex;
						align-items: center;
						height: 36rpx;
						padding: 0 18rpx;
						background: rgba(0, 0, 0, 0.5);
						border-radius: 18rpx;
						font-size: 22rpx;
						font-weight: 400;
						color: #dbdbdb;
						.point {
							width: 6rpx;
							height: 6rpx;
							margin-right: 6rpx;
							background-color: #69bdd6;
							border-radius: 50%;
						}
					}
					.position {
						.cl-icon {
							margin-right: 6rpx;
						}
						font-size: 22rpx;
						font-weight: 400;
						color: #dbdbdb;
					}
				}
				.avatar-group {
					display: flex;
					align-items: center;
					position: absolute;
					left: 0;
					bottom: 24rpx;
					padding: 0 24rpx;
					.avatar {
						position: relative;
						width: 66rpx;
						height: 66rpx;
						margin-right: 12rpx;
						.img {
							width: 100%;
							height: 100%;
							border-radius: 50%;
						}
						.man {
							display: flex;
							justify-content: center;
							align-items: center;
							position: absolute;
							right: 0;
							bottom: 0;
							width: 26rpx;
							height: 26rpx;
							background-color: #58bcff;
							border-radius: 50%;
							image {
								width: 18rpx;
								height: 18rpx;
							}
						}
						.woman {
							display: flex;
							justify-content: center;
							align-items: center;
							position: absolute;
							right: 0;
							bottom: 0;
							width: 26rpx;
							height: 26rpx;
							background-color: #ff5a70;
							border-radius: 50%;
							image {
								width: 18rpx;
								height: 18rpx;
							}
						}
					}
					.name {
						font-size: 24rpx;
						font-weight: 400;
						color: #ffffff;
					}
				}
				&-img {
					width: 330rpx;
					height: 330rpx;
					border-radius: 16rpx 16rpx 0 0;
				}
				.mask {
					position: absolute;
					top: 0;
					left: 0;
					width: 100%;
					height: 100%;
					background: rgba(0, 0, 0, 0.5);
					z-index: 2;
				}
			}
			.bottom {
				padding: 0 24rpx;
				.title {
					display: block;
					margin-bottom: 16rpx;
					font-size: 32rpx;
					font-weight: 500;
					color: #282828;
				}
				.group {
					.content {
						.tag {
							display: inline-flex;
							align-items: center;
							height: 42rpx;
							margin-right: 18rpx;
							padding: 0 15rpx;
							background: linear-gradient(90deg, #ff819b 0%, #ff5b71 100%);
							border-radius: 27rpx;
							font-size: 24rpx;
							font-weight: 400;
							color: #ffffff;
						}
						.subtitle {
							font-size: 24rpx;
							font-weight: 400;
							color: #b1b1b1;
						}
					}
				}
			}
		}
	}
	.floot {
		display: flex;
		justify-content: center;
		align-items: center;
		position: fixed;
		right: 30rpx;
		bottom: calc(50px + 90rpx);
		width: 100rpx;
		height: 100rpx;
		background: linear-gradient(90deg, #76b1bb 0%, #4d8b9d 100%);
		border-radius: 50%;
		z-index: 99;
		image {
			width: 42rpx;
			height: 42rpx;
		}
	}
}
</style>
